﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Modeling_Sub_Pages_NewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WelcomeUser"] != null)
        {
            if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("EAS"))
            {
                UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

                // Ko chay lai doan nay khi postback
                if (!IsPostBack)
                {
                    ddlTeacher.DataSource = udm.showTeacher(udm.createConnection());
                    ddlTeacher.DataTextField = "Full Name";
                    ddlTeacher.DataValueField = "ID";
                    ddlTeacher.DataBind();

                    ddlSubject.DataSource = udm.showSubject(udm.createConnection());
                    ddlSubject.DataTextField = "Name";
                    ddlSubject.DataValueField = "ID";
                    ddlSubject.DataBind();
                }
            }
            else
            {
                Response.Redirect("~/LoginMessage.aspx?id=1");
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx?id=2");
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        DateTime date = Calendar1.SelectedDate;
        txtDateTime.Text = date.ToString("dd-MM-yyyy");
        Calendar1.Visible = false;
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtTitle.Text = "";
        txtDateTime.Text = "";
        txtLocation.Text = "";
        txtDescription.Text = "";
        txtHour.Text = "";
        txtMinute.Text = "";
        txtHourEnd.Text = "";
        txtMinuteEnd.Text = "";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();
        
        string name = txtTitle.Text;
        string subjectID = ddlSubject.SelectedValue.ToString();
        int teacherID = Convert.ToInt32(ddlTeacher.SelectedValue.ToString());
        string location = txtLocation.Text;
        string content = txtDescription.Text;

        int hour = Convert.ToInt32(txtHour.Text);
        int minute = Convert.ToInt32(txtMinute.Text);

        DateTime datestart = new DateTime(Calendar1.SelectedDate.Year, Calendar1.SelectedDate.Month, Calendar1.SelectedDate.Day, hour, minute, 0);

        int hourEnd = Convert.ToInt32(txtHourEnd.Text);
        int minuteEnd = Convert.ToInt32(txtMinuteEnd.Text);

        DateTime dateEnd = new DateTime(Calendar1.SelectedDate.Year, Calendar1.SelectedDate.Month, Calendar1.SelectedDate.Day, hourEnd, minuteEnd, 0);

        bool monday = cbMonday.Checked;
        bool tuesday = cbTuesday.Checked;
        bool wednesday = cbWednesday.Checked;
        bool thursday = cbThursday.Checked;
        bool friday = cbFriday.Checked;
        bool saturday = cbSaturday.Checked;
        bool sunday = cbSunday.Checked;

        if (DateTime.Compare(DateTime.Today, datestart) <= 0)
        {
            if (hourEnd > hour)
            {
                if (udm.addNewCourse(udm.createConnection(), name, subjectID, teacherID, datestart, location, content))
                {
                    int idCourse = udm.getLastestCourseID(udm.createConnection());

                    if (udm.addTimeTable(udm.createConnection(), idCourse, monday, tuesday, wednesday, thursday, friday, saturday, sunday, datestart, dateEnd))
                    {
                        Response.Redirect("MaintainMessage.aspx?ID=10");
                    }
                    else
                    {
                        Response.Redirect("MaintainMessage.aspx?ID=11");
                    }
                }
                else {
                    Response.Redirect("MaintainMessage.aspx?ID=11");
                }
            }
        }     
    }
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        DateTime datestart = Calendar1.SelectedDate;

        if (DateTime.Compare(DateTime.Today, datestart) <= 0)
        {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }

    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        int hourStart = Convert.ToInt32(txtHour.Text);
        int hourEnd = Convert.ToInt32(txtHourEnd.Text);

        if (hourEnd > hourStart)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
}